package com.peng.immodule.im;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.peng.immodule.entity.Contact;
import com.peng.immodule.util.CryptoUtils;

/**
 * Created by M_genius on 2018/3/6 15:26.
 * 描述：IM 会话管理器
 *  1、主要是存取用户的 UserName、Pwd 以及登陆时是否带有 Token 信息，数据都是通过 SharedPreferences 存取
 *  2、采用单例模式获取对象
 */

public class IMSessionManager {

    private static final String TAG = "Session";
    private Context appContext = null;

    /**
     * 保存账户的 key，数据保存在 SharedPreferences
     */
    private static final String PREF_KEY_LOGIN_USER = "im.chat.loginUser";

    /**
     * 保存密码的 key，数据保存在 SharedPreferences
     */
    private static final String PREF_KEY_LOGIN_PWD = "im.chat.loginPwd";

    /**
     * 保存带 Token 的登录信息的 key ，数据保存在 SharedPreferences
     */
    private static final String PREF_KEY_LOGIN_WITH_TOKEN = "im.chat.login_with_token";

    public Contact currentUser = null;

    private static IMSessionManager instance = new IMSessionManager();

    private String lastLoginUser = null;
    private String lastLoginPwd = null;

    IMSessionManager() {
    }

    static synchronized IMSessionManager getInstance() {
        if(instance.appContext == null) {
            instance.appContext = IMClient.getInstance().getContext();
        }

        return instance;
    }

    /**
     * 从 SharedPreferences 获取登录的账户信息
     * @return
     */
    String getLastLoginUser() {
        if(this.lastLoginUser == null) {
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.appContext);
            this.lastLoginUser = sharedPreferences.getString("im.chat.loginUser", "");
            this.currentUser = new Contact(this.lastLoginUser);
        }

        return this.lastLoginUser;
    }

    /**
     * 将当前登录的账户存入 SharedPreferences
     * @param userName
     */
    void setLastLoginUser(String userName) {
        if(userName != null) {
            this.currentUser = new Contact(userName);
            this.lastLoginUser = userName;
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.appContext);
            Editor editor = sharedPreferences.edit();
            editor.putString("im.chat.loginUser", userName);
            editor.apply();
        }
    }

    /**
     * 从 SharedPreferences 获取登录密码
     * @return
     */
    String getLastLoginPwd() {
        if(lastLoginPwd == null) {
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.appContext);
            String loginPwd = sharedPreferences.getString("im.chat.loginPwd", "");
            // 如果密码不存在，则直接返回 ""
            if(loginPwd.equals("")) {
                lastLoginPwd = "";
                return lastLoginPwd;
            }

            try {
                CryptoUtils cryptoUtils = IMClient.getInstance().getCryptoUtils();
                // 将取出的密码解密后返回
                lastLoginPwd = cryptoUtils.decryptBase64String(loginPwd);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        return lastLoginPwd;
    }

    /**
     * 将上次登陆的账户信息置空
     */
    void clearLastLoginUser() {
        try {
            lastLoginUser = "";
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.appContext);
            Editor editor = sharedPreferences.edit();
            editor.putString("im.chat.loginUser", lastLoginUser);
            editor.apply();
        } catch (Exception exception) {
            ;
        }

    }

    /**
     * 将上次登陆的密码信息置空
     */
    void clearLastLoginPwd() {
        try {
            this.lastLoginPwd = "";
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.appContext);
            Editor editor = sharedPreferences.edit();
            editor.putString("im.chat.loginPwd", this.lastLoginPwd);
            editor.apply();
        } catch (Exception exception) {
            ;
        }

    }

    /**
     * 将上次登陆的账户密码进行加密后存入 SharedPreferences
     *
     * @param pwd
     */
    void setLastLoginPwd(String pwd) {
        if(pwd != null) {
            lastLoginPwd = pwd;
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.appContext);
            Editor editor = sharedPreferences.edit();

            try {
                CryptoUtils cryptoUtils = IMClient.getInstance().getCryptoUtils();
                String encryptPwd = cryptoUtils.encryptBase64String(pwd);
                editor.putString("im.chat.loginPwd", encryptPwd);
                editor.apply();
            } catch (Exception exception) {
                exception.printStackTrace();
            }

        }
    }

    /**
     * 判断上次登陆是否带有 Token 信息，数据也是从 SharedPreferences 获取
     *
     * @return
     */
    boolean isLastLoginWithToken() {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.appContext);
        return sharedPreferences.getBoolean("im.chat.login_with_token", false);
    }

    /**
     * 登陆时，若带有 Token 信息，则在 SharedPreferences 存储一个标记
     *
     * @param isLoginWithToken
     */
    void setLastLoginWithToken(boolean isLoginWithToken) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.appContext);
        Editor editor = sharedPreferences.edit();
        editor.putBoolean("im.chat.login_with_token", isLoginWithToken);
        editor.apply();
    }

    /**
     * 获取用户的 userName
     *
     * @return
     */
    public String getLoginUserName() {
        return this.currentUser.username;
    }

}
